﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace SimpleGameData.Animations
{
    public class AnimationData
    {
        #region Fields

        // The amount of frames the animation consists of
        private int framecount;
        // The width in pixel needed to calculate the frames of the animation
        private int frameWidth;
        // The hight in pixel needed to calculate the frames of the animation
        private int frameHeight;
        // The amount of frames shown per second
        private int framesPerSecond;

        #endregion

        #region Properties

        /// <summary>
        /// The amount of frames the animation consists of.
        /// </summary>
        public int FrameCount
        {
            get { return framecount; }
            set { framecount = value; }
        }

        /// <summary>
        /// The amount of frames shown per second. Clamped to 1 to 60 because less than 1 makes no sense
        /// and the game does 60 draws per second as a maximum.
        /// </summary>
        public int FramesPerSecond
        {
            get { return framesPerSecond; }
            set
            {
                if (value < 1)
                    framesPerSecond = 1;
                else if (value > 60)
                    framesPerSecond = 60;
                else
                    framesPerSecond = value;
            }
        }

        /// <summary>
        /// The width of the frame in pixel
        /// </summary>
        public int FrameWidth
        {
            get { return frameWidth; }
            set { frameWidth = value; }
        }

        /// <summary>
        /// The hight of the frame in pixel
        /// </summary>
        public int FrameHeight
        {
            get { return frameHeight; }
            set { frameHeight = value; }
        }

        #endregion

        #region Constructor

        public AnimationData() { }

        #endregion
    }

    #region ContentTypeReader Class

    public class AnimationDataReader : ContentTypeReader<AnimationData>
    {
        protected override AnimationData Read(ContentReader input,
            AnimationData existingInstance)
        {
            AnimationData animationData = existingInstance;

            if (animationData == null)
            {
                animationData = new AnimationData();
            }

            animationData.FrameCount = input.ReadInt32();
            animationData.FrameWidth = input.ReadInt32();
            animationData.FrameHeight = input.ReadInt32();
            animationData.FramesPerSecond = input.ReadInt32();

            return animationData;
        }
    }

    #endregion
}
